package com.bdqn.shiro.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.shiro.pojo.SalChance;
import com.bdqn.shiro.service.SalChanceService;
import com.bdqn.shiro.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("chance")
public class ChanceController {

    @Resource
    private SalChanceService salChanceService;
    @Resource
    private UserService userService;

    @RequestMapping("list")
    public String List(Model model, SalChance salChance, @RequestParam(defaultValue = "1") Long pageIndex) {
        Page page = new Page();
        page.setCurrent(pageIndex);
        page.setRecords(salChanceService.getList(page, salChance));
        model.addAttribute("page", page);
        return "chance/list";
    }

    @GetMapping("edit")
    public String edit(Model model, SalChance salChance) {
        SalChance sal = salChanceService.getById(salChance);
        model.addAttribute("chance", sal);
        model.addAttribute("users", userService.list());
        return "chance/edit";
    }

    @GetMapping("add")
    public String toAdd(Model model) {
        model.addAttribute("users", userService.list());
        return "chance/add";
    }

    @RequestMapping("save")
    public String save(SalChance salChance) {
        salChanceService.saveOrUpdate(salChance);
        return "redirect:/chance/list";

    }

    @ResponseBody
    @GetMapping("del/{chcId}")
    public Map del(@PathVariable("chcId") Long chcId) {
        Map map = new HashMap();
        boolean bo = salChanceService.removeById(chcId);
        if (bo) {
            map.put("delResult", "true");
        }

        return map;
    }
}
